﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;

namespace YXShop.Web.admin.templates.lable
{
    public partial class labelclass_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("011002001");
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormInt("id") > 0)
                {
                    YXShop.Common.PromptInfo.Popedom("011002003", "对不起，您没有权限进行删除");
                    string types = Request.Form["Option"].Trim();
                    int id = ChangeHope.WebPage.PageRequest.GetFormInt("id");
                    string path = ChangeHope.WebPage.PageRequest.GetFormString("path");
                    if (types == "del")
                    {
                        del(id,path);
                    }
                    Response.End();
                    return;
                }
                GetList();
                string IsStoreLabel = ChangeHope.WebPage.PageRequest.GetQueryString("w_d_isstorelabel");
                string templatepath = ChangeHope.WebPage.PageRequest.GetQueryString("q_templatepath");
                if (IsStoreLabel != "-1")
                {
                    this.lnkAddChannel.NavigateUrl = "labelclass_edit.aspx?templatepath=" + templatepath + "&IsStoreLabel=" + IsStoreLabel;
                }
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        protected void GetList()
        {
            string IsStoreLabel = ChangeHope.WebPage.PageRequest.GetQueryString("w_d_isstorelabel");
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            //YXShop.BLL.Templates.LabelType data = new YXShop.BLL.Templates.LabelType();
            YXShop.BLL.Templates.Label lbll = new YXShop.BLL.Templates.Label();
            //ChangeHope.DataBase.DataByPage dataPage = data.GetList();
            //第一步先添加表头
            table.AddHeadCol("5%", "序号");
            table.AddHeadCol("60%", "分类名称");
            table.AddHeadCol("20%", "更新时间");
            table.AddHeadCol("15%", "操作");
            table.AddRow();
            string labelTypeId = ChangeHope.WebPage.PageRequest.GetQueryString("q_id");
            string templatePath = ChangeHope.WebPage.PageRequest.GetQueryString("q_templatepath");
            string path = Server.MapPath("~//" + templatePath + "/label/labeltype.xml");
            if (File.Exists(path))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path);
                XmlNode xn = xmlDoc.SelectSingleNode("labeltypegroup");
                XmlNodeList xnl = xn.ChildNodes;
                if (xnl.Count > 0)
                {
                    int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                    if (curpage < 0)
                    {
                        curpage = 1;
                    }
                    int count = 0;
                    foreach (XmlNode xnf in xnl)
                    {
                        count++;
                        string No = (15 * (curpage - 1) + count).ToString();
                        table.AddCol(No);
                        XmlElement xe = (XmlElement)xnf;
                        XmlNodeList xnf1 = xe.ChildNodes;
                        foreach (XmlNode xn2 in xnf1)
                        {
                            if (xn2.Name == "name")
                            {
                                table.AddCol(string.Format("<a href='label_list.aspx?q_id={2}&q_templatepath={1}&q_name={0}'>{0}</a>", xn2.InnerText, templatePath, xe.GetAttribute("id")));
                                continue;
                            }
                            if (xn2.Name == "creattime")
                            {
                                table.AddCol(xn2.InnerText);
                                continue;
                            }
                        }
                        table.AddCol(string.Format("<a href=labelclass_edit.aspx?id={0}&IsStoreLabel={1}&templatepath={2}>编辑</a> <a href='#' onclick=\"Del({0},'{2}')\">删除</a>", xe.GetAttribute("id"), IsStoreLabel, templatePath));
                        table.AddRow();
                    }

                }
            }
            //// 添加表的内容
            // if (dataPage.DataReader != null)
            // {
            //     int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
            //     if (curpage < 0)
            //     {
            //         curpage = 1;
            //     }
            //     int count = 0;
            //     while (dataPage.DataReader.Read())
            //     {
            //         count++;
            //         string No = (15 * (curpage - 1) + count).ToString();
            //         table.AddCol(No);
            //         table.AddCol(string.Format("<a href='label_list.aspx?q_styleId={4}&q_name={1}&w_d_tid={0}&w_d_isstorelabel={3}'>{1}</a><span style='color:red;'>({2})</span>", dataPage.DataReader["id"].ToString(), dataPage.DataReader["name"].ToString(), lbll.GetCount(Convert.ToInt32(dataPage.DataReader["id"].ToString())), IsStoreLabel, dataPage.DataReader["styleid"].ToString()));
            //         if (dataPage.DataReader["id"].ToString() == "1")
            //         {
            //             table.AddCol("所有风格");
            //         }
            //         else
            //         {
            //             table.AddCol(dataPage.DataReader["StyleName"].ToString());
            //         }
            //         table.AddCol(dataPage.DataReader["creattime"].ToString());
            //         if (dataPage.DataReader["id"].ToString() == "1")
            //         {
            //             table.AddCol(string.Format("<a href=labelclass_edit.aspx?id={0}&IsStoreLabel={1}>编辑</a>", dataPage.DataReader["id"].ToString(), IsStoreLabel));
            //         }
            //         else
            //         {
            //             table.AddCol(string.Format("<a href=labelclass_edit.aspx?id={0}&IsStoreLabel={1}>编辑</a> <a href='#' onclick='Del({0})'>删除</a>", dataPage.DataReader["id"].ToString(), IsStoreLabel));
            //         }

            //         table.AddRow();
            //     }
            // }
            //+ dataPage.PageToolBar
            string view = table.GetTable();
            //dataPage.Dispose();
            //dataPage = null;
            this.Literal1.Text = view;
        }

        protected void del(int id, string templatePath)
        {
            string path = Server.MapPath("~//" + templatePath + "/label/labeltype.xml");
            if (File.Exists(path))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path);
                XmlNode root = xmlDoc.SelectSingleNode("labeltypegroup");
                XmlNodeList xnList = root.ChildNodes;
                foreach (XmlNode xn in xnList)
                {
                    XmlElement xe = (XmlElement)xn;
                    string nodeId = xe.GetAttribute("id");
                    if (nodeId == id.ToString())
                    {
                        root.RemoveChild(xn);// 移除指定的子节点
                        break;
                    }
                }
                xmlDoc.Save(path);
                Response.Write("ok");
            }
        }
    }
}
